<template>
	<!-- 补发/入库记录 -->
	<a-modal :title="title" :width="1400" :visible="visible" :confirmLoading="confirmLoading" :footer="null"
		@cancel="handleCancel">
		<a-spin :spinning="formLoading">
			<s-table ref="table" style="margin-top: 15px;" size="default" :columns="columns" :data="loadData" :alert="true">
				<span slot="action" slot-scope="text,record">
					<a @click="$refs.reissueOrStoreDetailsOut.init(dialogType, record)">{{ dialogType == 'reissue' ? '补发明细' :
						'入库明细'
						}}</a>
				</span>
			</s-table>
		</a-spin>
		<reissueOrStoreDetailsOut ref="reissueOrStoreDetailsOut" />
	</a-modal>
</template>

<script>
import { STable } from '@/components'
import { emsDefectivePoolReissueRecord, rkPage } from '@/api/modular/main/badPool'
import { page } from '@/api/modular/main/emsorder/emsOrderManage'
import { getUrlParam } from '@/utils/util'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import Vue from 'vue'
// import { page as rkPage } from '@/api/modular/main/emsorder/emsOrderManage'; //入库记录
import reissueOrStoreDetailsOut from './reissueOrStoreDetailsOut.vue'

import { emsDefectivePoolReissueMaterial } from '@/api/modular/main/badPool'

import { page as rkInfo } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'

export default {
	components: {
		STable,
		reissueOrStoreDetailsOut
	},
	props: ['materialType'],
	data() {
		return {
			dialogType: '',
			labelCol: {
				xs: { span: 24 },
				sm: { span: 5 }
			},
			wrapperCol: {
				xs: { span: 24 },
				sm: { span: 15 }
			},
			orgTree: [],
			visible: false,
			formLoading: false,
			confirmLoading: false,
			route_path: '',
			columns: [],
			title: '',
			columns1: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '时间',
					dataIndex: 'createTime'
				},
				{
					title: '补发编号',
					dataIndex: 'number'
				},
				{
					title: '操作人',
					dataIndex: 'createUserName'
				},
				{
					title: '操作',
					align: 'center',
					width: '250px',
					scopedSlots: { customRender: 'action' }
				}
			],
			columns2: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '时间',
					dataIndex: 'createTime'
				},
				{
					title: '入库编号',
					dataIndex: 'orderNumber'
				},
				{
					title: '送库单号',
					dataIndex: 'defectiveSendOrderNumber'
				},
				{
					title: '操作人',
					dataIndex: 'createUserName'
				},
				{
					title: '操作',
					align: 'center',
					width: '250px',
					scopedSlots: { customRender: 'action' }
				}
			],
			columns3: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '时间',
					align: 'center',
					dataIndex: 'createTime'
				},
				{
					title: '补发编号',
					align: 'center',
					dataIndex: 'reissueNumber'
				},
				{
					title: '补发数量',
					align: 'center',
					dataIndex: 'num'
				},
				{
					title: '操作人',
					align: 'center',
					dataIndex: 'createUserName'
				},
			],
			columns4: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '时间',
					align: 'center',
					dataIndex: 'createTime'
				},
				{
					title: '入库编号',
					align: 'center',
					dataIndex: 'orderNumber'
				},
				{
					title: '送库单号',
					dataIndex: 'defectiveSendOrderNumber'
				},
				{
					title: '入库数量',
					align: 'center',
					dataIndex: 'number'
				},
				{
					title: '操作人',
					align: 'center',
					dataIndex: 'createUserName'
				},
			],
			queryParam: {},
			loadData: parameter => {
				if (this.dialogType == "reissue") {
					let query = {
						...parameter,
						...this.queryParam
					}
					return emsDefectivePoolReissueRecord(query).then((res) => {
						return res.data
					})
				} else if (this.dialogType == "store") {
					let query = {
						...parameter,
						...this.queryParam
					}
					return rkPage(query).then((res) => {
						return res.data
					})
				} else if (this.dialogType == "storeIn") {//入库记录
					let query = {
						...parameter,
						...this.queryParam
					}
					return rkInfo(query).then((res) => {
						return res.data
					})
				} else {//补发记录
					let query = {
						...parameter,
						...this.queryParam
					}
					return emsDefectivePoolReissueMaterial(query).then((res) => {
						return res.data
					})
				}

			},
		}
	},
	created() {
		this.route_path = getUrlParam(this.$route.path).materialType
		console.log("route_path", this.route_path);
	},
	methods: {
		// 初始化方法
		init(dialogType, record) {
			console.log("----------",record);
			this.dialogType = dialogType
			this.visible = true;
			if (this.dialogType == "reissue") {
				this.columns = this.columns1;
				this.title = "补发记录"
				this.queryParam.defectivePoolRecordId = record.id;
			} else if (this.dialogType == "store") {
				this.columns = this.columns2;
				this.title = "入库记录"
				this.queryParam.materialType = this.route_path;
				this.queryParam.sourceOrderId = record.id;
				this.queryParam.orderType = "1479446404789526530"
			} else if (this.dialogType == "reissueIn") {
				this.columns = this.columns3;
				this.title = "补发记录"
				this.queryParam.defectivePoolMaterialId = record.id;
			} else if (this.dialogType == "storeIn") {
				this.columns = this.columns4;
				this.title = "入库记录";
				this.queryParam.sourceOrderMaterialId = record.id
			}
			setTimeout(() => {
				this.$refs.table.refresh();
			}, 500);
		},
		handleSubmit() {
			let text = this.dialogType == 'edit' ? '编辑' : '新增'
			const { form: { validateFields } } = this
			validateFields((errors, values) => {
				if (!errors) {
					add({ ...values, materialType: this.materialType }).then((res) => {
						if (res.success) {
							this.$message.success(`${text}成功`)
							this.visible = false
							this.confirmLoading = false
							this.$emit('ok', values)
							this.form.resetFields()
							this.fileList = []
							this.selectedObj = {}
						} else {
							this.$message.error(`${text}失败` + res.message)
						}
					}).finally((res) => {
						this.confirmLoading = false
					})
				} else {
					this.confirmLoading = false
				}
			})
		},
		handleCancel() {
			this.visible = false
		},
	}
}
</script>

<style>
.inputDiv {
	width: 100%;
	line-height: 32px;
	cursor: pointer;
	background: #eee;
	border: 1px solid #bbb;
	padding-left: 10px;
}
</style>
